package com.yl.chainStore.util

import com.yl.chainStore.common.enums.TypeEnum
import java.text.DateFormat
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*

class DateUtil {


}

private val timeSdf = SimpleDateFormat("HH:mm")
private val daySdf = SimpleDateFormat("MM月dd日")
private val yearSdf = SimpleDateFormat("yyyy")
private val dateSdf = SimpleDateFormat("yyyy-MM-dd")
private val dateTimeSdf = SimpleDateFormat("yyyy-MM-dd HH:mm")


fun yearFormat(time: Date): String {
	return yearSdf.format(time)
}

fun dateFormat(time: Date): String {
	return dateSdf.format(time)
}

fun dayFormat(time: Date): String {
	return daySdf.format(time)
}

fun timeFormat(time: Date): String {
	return timeSdf.format(time)
}

fun dateTimeFormat(time: Date): String {
	return dateTimeSdf.format(time)
}

/**
 * 获取某一周 的周一  yyyy-MM-dd 00:00:00
 */
fun getMonday(day: Date): Date {
	val cal = Calendar.getInstance()
	cal.time = day
	//设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
	cal.firstDayOfWeek = Calendar.MONDAY
	//获得当前日期是一个星期的第几天
	var dayWeek = cal[Calendar.DAY_OF_WEEK]
	if (dayWeek == 1) {
		dayWeek = 8
	}
	// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
	cal.add(Calendar.DATE, cal.firstDayOfWeek - dayWeek)
	cal[Calendar.HOUR_OF_DAY] = 0
	cal[Calendar.MINUTE] = 0
	cal[Calendar.SECOND] = 0
	cal[Calendar.MILLISECOND] = 0
	return cal.time
}

/**
 * 获取某一周 的周日  yyyy-MM-dd 23:59:59
 */
fun getSunDay(day: Date): Date {
	//本周
	val cal = Calendar.getInstance()

	cal.time = day
	cal.firstDayOfWeek = Calendar.MONDAY
	//获得当前日期是一个星期的第几天
	var dayWeek = cal[Calendar.DAY_OF_WEEK]
	if (dayWeek == 1) {
		dayWeek = 8
	}
	cal.add(Calendar.DATE, cal.firstDayOfWeek - dayWeek)
	cal.add(Calendar.DATE, 4 + cal.firstDayOfWeek)
	cal.set(Calendar.HOUR_OF_DAY, 23)
	cal.set(Calendar.MINUTE, 59)
	cal.set(Calendar.SECOND, 59)
	cal.set(Calendar.MILLISECOND, 59)
	return cal.time
}

/**
 * 获取某月的第一天  yyyy-MM-dd 00:00:00
 */
fun getFirstDayByMonth(day: Date): Date {
	val cal = Calendar.getInstance()
	cal.time = day

	// 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
	cal[Calendar.DAY_OF_MONTH] = 1
	cal[Calendar.HOUR_OF_DAY] = 0
	cal[Calendar.MINUTE] = 0
	cal[Calendar.SECOND] = 0
	cal[Calendar.MILLISECOND] = 0
	return cal.time
}


/**
 * 获取某月的最后一天  yyyy-MM-dd 23:59:59
 */
fun getLastDayByMonth(day: Date): Date {
	//本周
	val cal = Calendar.getInstance()

	cal.time = day

	cal.set(Calendar.DAY_OF_MONTH,cal.getActualMaximum(Calendar.DAY_OF_MONTH))
	cal.set(Calendar.HOUR_OF_DAY, 23)
	cal.set(Calendar.MINUTE, 59)
	cal.set(Calendar.SECOND, 59)
	cal.set(Calendar.MILLISECOND, 59)
	return cal.time
}


/**
 * 日期的早上时间
 */
fun setDayFirstTime(day: Date) {


	val cal = Calendar.getInstance()
	cal.time = day
	cal.set(Calendar.HOUR_OF_DAY, 0)
	cal.set(Calendar.MINUTE, 0)
	cal.set(Calendar.SECOND, 0)
	cal.set(Calendar.MILLISECOND, 0)
	day.time = cal.time.time
}

/**
 * 日期的最后一秒
 */
fun setDayLastTime(day: Date) {
	val cal = Calendar.getInstance()
	cal.time = day
	cal.set(Calendar.HOUR_OF_DAY, 23)
	cal.set(Calendar.MINUTE, 59)
	cal.set(Calendar.SECOND, 59)
	cal.set(Calendar.MILLISECOND, 59)
	day.time = cal.time.time
}

fun getDays(startTime: String, endTime: String): List<String> {

	// 返回的日期集合
	val days: MutableList<String> = ArrayList()
	val dateFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd")
	try {
		val start = dateFormat.parse(startTime)
		val end = dateFormat.parse(endTime)
		val tempStart = Calendar.getInstance()
		tempStart.time = start
		val tempEnd = Calendar.getInstance()
		tempEnd.time = end
		tempEnd.add(Calendar.DATE, +1) // 日期加1(包含结束)
		while (tempStart.before(tempEnd)) {
			days.add(dateFormat.format(tempStart.time))
			tempStart.add(Calendar.DAY_OF_YEAR, 1)
		}
	} catch (e: ParseException) {
		e.printStackTrace()
	}
	return days
}



fun getDays(start: Date, end: Date): MutableList<Date> {
	// 返回的日期集合
	val days: MutableList<Date> = ArrayList()
	try {
		val tempStart = Calendar.getInstance()
		tempStart.time = start
		val tempEnd = Calendar.getInstance()
		tempEnd.time = end
		tempEnd.add(Calendar.DATE, +1) // 日期加1(包含结束)
		while (tempStart.before(tempEnd)) {
			days.add(tempStart.time)
			tempStart.add(Calendar.DAY_OF_YEAR, 1)
		}
	} catch (e: ParseException) {
		e.printStackTrace()
	}
	return days
}


//毫秒转 n小时n分钟格式
fun formatDuring(mss: Long): String {
	//	val days = mss / (1000 * 60 * 60 * 24)
	val hours = ((mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)).toString()

	val minutes = ((mss % (1000 * 60 * 60)) / (1000 * 60)).toString()

	//	val seconds = (mss % (1000 * 60)) / 1000;
	return if (hours == "0") {
		minutes + "分钟"
	} else {
		hours + "小时" + minutes + "分钟"
	}
}

fun getDayAdd(day: Date, addDay: Int): Date {

	val cal = Calendar.getInstance()
	cal.time = day
	cal.add(Calendar.DATE, addDay)
	return cal.time
}

/**
 * 设置时间到最近的15分钟
 */
fun setTimeTo15Min(day: Date) {
	val cal = Calendar.getInstance()
	cal.time = day
	val get = cal.get(Calendar.MINUTE)
	cal.set(Calendar.SECOND, 0)
	cal.set(Calendar.MILLISECOND, 0)
	val moreMin = get % 15
	if (moreMin == 0) {
		day.time = cal.time.time
		return
	}
	var time = (get / 15)
	if (moreMin > 7) {
		time += 1
	}
	if (time == 4) {
		cal.add(Calendar.HOUR, 1)
		cal.set(Calendar.MINUTE, 0)
	} else {
		cal.set(Calendar.MINUTE, time * 15)
	}
	day.time = cal.time.time
	return
}

fun addTimeByType(time:Date,number:Int,type: TypeEnum):Date{
	val cal = Calendar.getInstance()
	cal.time = time
	when (type) {
		TypeEnum.SICK_CUSTOMER_ACCESS_LOG_MODEL_DETAIL_TIME_TYPE_DAY -> {
			cal.add(Calendar.DATE,number)
		}
		TypeEnum.SICK_CUSTOMER_ACCESS_LOG_MODEL_DETAIL_TIME_TYPE_WEEK -> {
			cal.add(Calendar.WEEK_OF_MONTH,number)
		}
		else -> {
			cal.add(Calendar.MONTH,number)
		}
	}
	return cal.time

}




